package com.news.ui.main.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.news.R;
import com.news.base.BaseActivity;
import com.news.ui.main.fragment.JokeFragment;
import com.news.ui.main.fragment.NewsFragment;
import com.news.ui.main.fragment.SettingFragment;
import com.news.ui.main.fragment.VideoFragment;
import com.news.util.LogUtil;

import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.main_bottom_tab_parent)
    RadioGroup mTabParent;

    @Override
    public int getLayoutId() {
        setHasProgress(false);
        return R.layout.activity_main;
    }

    @Override
    public void initViews() {
        mTabParent.setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.main_bottom_tab_joke)).setChecked(true);
    }

    @Override
    public void loadData() {
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        String tag = String.valueOf(checkedId);
        List<Fragment> fragments = supportFragmentManager.getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                fragmentTransaction.hide(fragment);
            }
        }

        Fragment fragment = supportFragmentManager.findFragmentByTag(tag);
        if (fragment == null) {
            fragment = ensureNotNull(checkedId);
            fragmentTransaction.add(R.id.main_container, fragment, tag);
        }
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
    }

    private Fragment ensureNotNull(int checkedId) {
        Fragment fragment = null;
        switch (checkedId) {
            case R.id.main_bottom_tab_joke:
                fragment = new JokeFragment();
                LogUtil.d("MainActivity", "ensureNotNull: main_bottom_tab_joke");
                break;
            case R.id.main_bottom_tab_video:
                fragment = new VideoFragment();
                LogUtil.d("MainActivity", "ensureNotNull: main_bottom_tab_video");
                break;
            case R.id.main_bottom_tab_news:
                fragment = new NewsFragment();
                LogUtil.d("MainActivity", "ensureNotNull: main_bottom_tab_news");
                break;
            case R.id.main_bottom_tab_setting:
                fragment = new SettingFragment();
                LogUtil.d("MainActivity", "ensureNotNull: main_bottom_tab_setting");
                break;
        }
        return fragment;
    }
}
